package com.JFapp.service.impl;


import com.JFapp.annotation.validator.name.Unique;
import com.JFapp.domain.dto.EmpowerDto;
import com.JFapp.domain.entity.ResponseResult;
import com.JFapp.domain.entity.User;
import com.JFapp.eumns.AppHttpCodeEnum;
import com.JFapp.exception.SystemException;
import com.JFapp.service.UploadService;
import com.JFapp.service.UserService;
import com.JFapp.utils.ExcelReaderUtils;
import com.JFapp.utils.PathUtils;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.JFapp.oss.uploadOSS;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Service
public class UploadServiceImpl implements UploadService {

    @Autowired
    private uploadOSS uploadOSS;
    @Override
    public ResponseResult uploadImg(MultipartFile img) {
        //判断文件类型
        //获取原始文件名
        String filename = img.getOriginalFilename();
        //对原始文件名进行判断
        if(!filename.endsWith(".png")&&!filename.endsWith(".jpg")){
            throw new SystemException(AppHttpCodeEnum.FILE_TYPE_ERROR);
        }
        //若果判断通过，上传到阿里云OSS
        String filePath = PathUtils.generateFilePath(filename);
        String url=uploadOSS.uploadOSS(img,filePath);
        return ResponseResult.okResult(url);
    }


    @Override
    public List<EmpowerDto> uploadExcel(MultipartFile file ) throws IOException {
        List<EmpowerDto> EmpowerDTOS;
        try {
            InputStream inputStream = file.getInputStream();
            EmpowerDTOS = EasyExcel.read(inputStream)
                    .head(EmpowerDto.class)
                    .sheet()
                    .doReadSync();

        } catch (Exception e) {
            throw new SystemException(AppHttpCodeEnum.FILE_READ_ERROR);
        }
        return EmpowerDTOS;
    }


}
